package com.cbj.db_work.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cbj.db_work.common.result.R;
import com.cbj.db_work.entity.GameManage;
import com.cbj.db_work.entity.Player;
import com.cbj.db_work.entity.Team;
import com.cbj.db_work.entity.query.PlayerQuery;
import com.cbj.db_work.entity.query.TeamQuery;
import com.cbj.db_work.entity.vo.TeamVo;
import com.cbj.db_work.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.ws.rs.POST;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cbj
 * @since 2021-07-01
 */
@RestController
@RequestMapping("/db_work/team")
public class TeamController {
    @Autowired
    private TeamService teamService;
    @GetMapping("/gameManage")
    public R gameManage(){
        GameManage gameManage = teamService.setDManage();

        return R.ok().data("data",gameManage);
    }
    @GetMapping("/getAll")
    public R getAllTeam(){
        List<Team> list = teamService.list(null);
        return R.ok().data("list",list);
    }
    @PostMapping("/addTeam")
    public R addTeam(@RequestBody TeamVo teamVo){
        boolean flag = teamService.addTeam(teamVo);
        if(flag)
            return R.ok();
        return R.error();
    }
    @PostMapping("/pageCondition/{current}/{limit}")
    public R pageCondition(@PathVariable long current,
                           @PathVariable long limit,
                           @RequestBody(required = false) TeamQuery query){
        Page<Team> page = new Page<>(current, limit);
        QueryWrapper wrapper = new QueryWrapper();
        String id = query.getId();
        String name = query.getName();
        String begin = query.getBegin();
        String end = query.getEnd();
        String coach = query.getCoach();
        if(!StringUtils.isEmpty(id)){
            wrapper.eq("id",id);
        }
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("create_date",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("modified_date",end);
        }
        if(!StringUtils.isEmpty(coach)){
            wrapper.like("coach",coach);
        }
        teamService.page(page, wrapper);
        long total = page.getTotal();
        List<Team> records = page.getRecords();
        if(records == null){
            return R.error();
        }
        List<Team> voList = new ArrayList<>();
        for (Team record : records) {
            voList.add(record);
        }
        return R.ok().data("total", total).data("rows", voList);
    }

}

